package com.limbo.showapp.adapter;

import android.content.Context;
import android.media.Image;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.limbo.showapp.R;
import com.limbo.showapp.bean.PictureBean;
import com.limbo.showapp.bean.VideoBean;
import com.limbo.showapp.constant.AppConstant;
import com.limbo.showapp.widget.OnMultiClickListener;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

/**
 * Created by wangqi on 2018/9/11.
 */

public class VideoAdapter extends CommonAdapter<VideoBean.Video> {

    public VideoAdapter(Context context) {
        super(context, R.layout.item_video_list, new ArrayList<VideoBean.Video>());
    }

    public void setData(List<VideoBean.Video> list) {
        List<VideoBean.Video> datas = getDatas();
        datas.clear();
        datas.addAll(list);
        notifyDataSetChanged();
    }

    public void addData(List<VideoBean.Video> list) {
        List<VideoBean.Video> datas = getDatas();
        datas.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    protected void convert(final ViewHolder holder, VideoBean.Video video, final int position) {
        ImageView iv_over = holder.getView(R.id.iv_over);
        TextView tv_title = holder.getView(R.id.tv_title);
        TextView tv_time = holder.getView(R.id.tv_time);
        TextView tv_count = holder.getView(R.id.tv_count);

        //压缩图片
        RequestOptions options = new RequestOptions()
//                .placeholder(R.drawable.img_loading)
//                .error(R.drawable.img_error)
                //设置图片加载的优先级
                .priority(Priority.HIGH)
                //缓存策略,跳过内存缓存【此处应该设置为false，否则列表刷新时会闪一下】
                .skipMemoryCache(false)
                //缓存策略,硬盘缓存-仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的）
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(mContext).load(AppConstant.Url.BASE_URL + video.getImgUrl())
                .transition(withCrossFade())
                .thumbnail(0.1f)
                .apply(options)
                .into(iv_over);

        tv_title.setText(video.getTitle());
        tv_time.setText(video.getUpdateTime());
        tv_count.setText(video.getUpCount());

        holder.itemView.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                if (mOnItemClickListener != null)
                    mOnItemClickListener.onItemClick(v, holder, position);
            }
        });
    }
}
